package com.cross.util;

import java.util.List;

import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.dom4j.io.DOMReader;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.xml.sax.EntityResolver;

public class XMLUtil {
	private DOMReader domReader;
	private SAXReader saxReader;

	public XMLUtil() {
	}

	public SAXReader createSAXReader(String file, List errorsList,
			EntityResolver entityResolver) {
		if (saxReader == null)
			saxReader = new SAXReader();
		saxReader.setEntityResolver(entityResolver);
		saxReader.setMergeAdjacentText(true);
		saxReader.setValidation(true);
		return saxReader;
	}

	public DOMReader createDOMReader() {
		if (domReader == null)
			domReader = new DOMReader();
		return domReader;
	}

	public static Element generateDom4jElement(String elementName) {
		return DocumentFactory.getInstance().createElement(elementName);
	}

	public static void dump(Element element) {
		try {
			OutputFormat outformat = OutputFormat.createPrettyPrint();
			XMLWriter writer = new XMLWriter(System.out, outformat);
			writer.write(element);
			writer.flush();
			System.out.println("");
		} catch (Throwable t) {
			System.out.println(element.asXML());
		}
	}

}
